<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of db_obj
 *
 * @author Juraj
 */
class db_obj {

    private $link = false;

    public function __construct() {
        $this->openConnection(DB_CONNECTION);
    }

    public function openConnection($setup) {
        $setup = explode(":", $setup);
        $this->link = mysql_pconnect($setup[0], $setup[1], $setup[2]);
        if ($this->link) {
            if (mysql_select_db($setup[3], $this->link)) {
                mysql_query("SET CHARACTER SET 'UTF8'", $this->link);
            } else {
                return new error_obj("Database is not select!", get_class(), __FUNCTION__);
            }
        }
        return $this->link;
    }

    public function query($sql) {
        if ($this->link) {
            $result = mysql_query($sql, $this->link);
            if (!$result) {
                return new error_obj($sql . ": " . mysql_error(), get_class(), __FUNCTION__);
            } else {
                return $result;
            }
        } else {
            return new error_obj('Link Id does not exist', get_class(), __FUNCTION__);
        }
    }

    public function insertUpdate($table, $data, $keys) {
        if ($this->link) {

            $sql = 'SELECT * FROM ' . $table;

            $where = ' WHERE ';
            if (empty($keys)) {
                $where .= '1=1';
            } else {
                foreach ($keys as $key => $value) {
                    $where .= "`" . $key . "` = '" . $value . "' AND ";
                }
                $where = substr($where, 0, strlen($where) - 5);
            }

            $sql = $sql . $where;

            $exist = $this->getRow($sql);

            base_obj::debug('exist', $exist);

            if (error_obj::isError($exist)) {
                return $exist;
            }

            if (empty($exist)) {
                return $this->insert($table, $data);
            } else {
                return $this->update($table, $data, $keys);
            }
        } else {
            return new error_obj('Link Id does not exist', get_class(), __FUNCTION__);
        }
    }

    public function insert($table, $data) {
        if ($this->link) {
            $sql = "INSERT INTO `" . $table . "` SET ";
            foreach ($data as $key => $value) {
                $sql .= "`" . $key . "` = '" . addslashes(htmlspecialchars($value)) . "', ";
            }
            $sql = substr($sql, 0, strlen($sql) - 2);
            $result = $this->query($sql);
            return $result;
        } else {
            return new error_obj('Link Id does not exist', get_class(), __FUNCTION__);
        }
    }

    public function update($table, $data, $keys) {
        if ($this->link) {
            $sql = "UPDATE `" . $table . "` SET ";
            foreach ($data as $key => $value) {
                $sql .= "`" . $key . "` = '" . addslashes(htmlspecialchars($value)) . "', ";
            }
            $sql = substr($sql, 0, strlen($sql) - 2);

            $where = ' WHERE ';
            if (empty($keys)) {
                $where .= '1=1';
            } else {
                foreach ($keys as $key => $value) {
                    $where .= "`" . $key . "` = '" . $value . "' AND ";
                }
                $where = substr($where, 0, strlen($where) - 5);
            }

            $sql = $sql . $where;

            base_obj::debug("update sql", $sql);

            $result = $this->query($sql);
            return $result;
        } else {
            return new error_obj('Link Id does not exist', get_class(), __FUNCTION__);
        }
    }

    public function select($sql, $mode = 'GET_ALL') {
        if ($this->link) {
            $result = $this->query($sql);
            if (error_obj::isError($result)) {
                error_obj::setError($result, UNEXCEPTED);
                return $result;
            }
            if ($mode == 'GET_ALL') {
                $return = array();
                while ($row = mysql_fetch_array($result)) {
                    $return[] = $row;
                }
                return $return;
            }
            if ($mode == 'GET_ROW') {
                $return = mysql_fetch_array($result);
                if ($return === false)
                    return array();
                return $return;
            }
            if ($mode == 'GET_COL') {
                $temp = array();
                while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
                    $temp[] = $row;
                }
                $return = array();
                foreach ($temp as $key => $value) {
                    $return[] = $value[0];
                }
                return $return;
            }
            if ($mode == 'GET_ONE') {
                $return = mysql_fetch_array($result, MYSQL_NUM);
                return $return[0];
            }
        } else {
            return new error_obj('Link Id does not exist', get_class(), __FUNCTION__);
        }
    }

    public function getAll($sql) {
        return $this->select($sql, 'GET_ALL');
    }

    public function getRow($sql) {
        return $this->select($sql, 'GET_ROW');
    }

    public function getCol($sql) {
        return $this->select($sql, 'GET_COL');
    }

    public function getOne($sql) {
        return $this->select($sql, 'GET_ONE');
    }

    public function exist($sql) {
        
    }

    public function lastInsertedId() {
        return mysql_insert_id();
    }

    public function pager($in_count, $page, $rowPerPage) {
        $count = $in_count / $rowPerPage;
        if ($count == 0)
            return $count;
        else {
            $count = ceil($count);
            $max = $count - 1;
            $pager = '';
            if ($page != 0) {
                $pager .= '<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=0">First</a>&nbsp;..|..&nbsp;';
            }
            if (($page-100) >= 0) {
                $pager .= '<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . ($page - 100) . '">-100</a>&nbsp;..|..&nbsp;';
            }
            if (($page-50) >= 0) {
                $pager .= '<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . ($page - 50) . '">-50</a>&nbsp;..|..&nbsp;';
            }
            if (($page-10) >= 0) {
                $pager .= '<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . ($page - 10) . '">-10</a>&nbsp;..|..&nbsp;';
            }
            if (($page-1) >= 0) {
                $pager .= '<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . ($page - 1) . '">Previous</a>&nbsp;..|..&nbsp;';
            }
            
            for ($i = 10; $i > 0; $i--) {
                if (($page - $i) >= 0)
                    $pager .= '<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . ($page - $i) . '">' . ($page - $i + 1) . '</a>';
            }
            
            $pager .= '<a class="activePage" href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . $page . '"><b>' . ($page + 1) . '</b></a>';
            
            for ($i = 1; $i < 11; $i++) {
                if (($page + $i) <= $max)
                    $pager .= '<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . ($page + $i) . '">' . ($page + $i + 1) . '</a>';
            }
            
            if (($page+1) <= $max) {
                $pager .= '&nbsp;..|..&nbsp;<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . ($page + 1) . '">Next</a>';
            }
            if (($page+10) <= $max) {
                $pager .= '&nbsp;..|..&nbsp;<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . ($page + 10) . '">+10</a>';
            }
            if (($page+50) <= $max) {
                $pager .= '&nbsp;..|..&nbsp;<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . ($page + 50) . '">+50</a>';
            }
            if (($page+100) <= $max) {
                $pager .= '&nbsp;..|..&nbsp;<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . ($page + 100) . '">+100</a>';
            }
            if ($page < $max) {
                $pager .= '&nbsp;..|..&nbsp;<a href="' . $_SERVER['PHP_SELF'] . '?input=' . $GLOBALS['input'] . '&page=' . ($max) . '">Last</a>';
            }
            
            return $pager;
        }
    }

}

?>
